$(function() {
    layui.use(['form','laydate','table'], function() {
            var form = layui.form, laydate = layui.laydate,
                table = layui.table;//载入table模块
            laydate.render({
                elem: '#begin_date',
                isInitValue: false,
            });
            laydate.render({
                elem: '#end_date',
                isInitValue: false,
            });

            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                //执行搜索重载
                goSearch();
                return false;
            });
        }
    );
    goSearch();
});
function goSearch() {
    var resultId = $("#resultId").val();
    getRecordData3d(resultId);
}

/**
 * 获取上报统计
 */
function getRecordData3d(resultId) {

    var url = ctx + "module/device/range/charts/result/data3d";
    $.ajax({
        type : "POST",
        url : url,
        data: {
            resultId: resultId
        },
        success : function(data) {
            console.log('data',data);
            var values = data.records;

            var dom = document.getElementById("echarts-line-chart-1");
            var lineChart = echarts.init(dom, 'dark');

            $.getScript(
                'https://fastly.jsdelivr.net/npm/simplex-noise@2.4.0/simplex-noise.js'
            ).done(function () {
                var noise = new SimplexNoise(Math.random);
                function generateData(theta, min, max) {
                    var data = [];
                    for (var i = 0; i <= values.length; i++) {
                        for (var j = 0; j <= values.length; j++) {
                            var value = noise.noise2D(parseFloat(values[i]) / 20, parseFloat(values[j])  / 20);
                            valMax = Math.max(valMax, value);
                            valMin = Math.min(valMin, value);
                            data.push([i, j, value * 2 + 4]);
                        }
                    }
                    return data;
                }

                var valMin = Infinity;
                var valMax = -Infinity;
                var data = generateData(2, -5, 5);
                console.log('data', data);
                console.log('valMin', valMin);
                console.log('valMax', valMax);

                $("#begin_date").val(data.beginDate);
                $("#end_date").val(data.endDate);
                var lineoption = {
                    visualMap: {
                        show: false,
                        min: 2,
                        max: 6,
                        inRange: {
                            color: [
                                '#313695', '#4575b4', '#74add1', '#abd9e9',  '#e0f3f8',  '#ffffbf',
                                '#fee090', '#fdae61',  '#f46d43', '#d73027', '#a50026'
                            ]
                        }
                    },
                    xAxis3D: {
                        type: 'value'
                    },
                    yAxis3D: {
                        type: 'value'
                    },
                    zAxis3D: {
                        type: 'value',
                        max: 10,
                        min: 0
                    },
                    grid3D: {
                        axisLine: {
                            lineStyle: { color: '#fff' }
                        },
                        axisPointer: {
                            lineStyle: { color: '#fff' }
                        },
                        viewControl: {
                            // autoRotate: true
                        },
                        light: {
                            main: {
                                shadow: true,
                                quality: 'ultra',
                                intensity: 1.5
                            }
                        }
                    },
                    series: [
                        {
                            type: 'bar3D',
                            data: data,
                            shading: 'lambert',
                            label: {
                                formatter: function (param) {
                                    return param.value[2].toFixed(1);
                                }
                            }
                        }
                    ]
                };
                lineChart.setOption(lineoption);
                $(window).resize(lineChart.resize);
            });
        }
    });
}
